package com.sunxiansheng.user.myenum;

import lombok.Getter;

import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Description: 枚举小优化，将枚举转换为map，然后根据code来获取枚举
 * @Author sun
 * @Create 2024/7/22 10:54
 * @Version 1.0
 */
@Getter
public enum MyEnum {

    ZFB_PAY(0, "支付宝支付"),
    WX_PAY(1, "微信支付"),
    BANK_PAY(2, "银行支付");

    private int code;
    private String desc;

    MyEnum(int code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    /**
     * 将枚举转换为map（静态初始化）
     */
    public static Map<Integer, MyEnum> channelEnumMap = Stream.of(MyEnum.values()).
            collect(Collectors.toMap(MyEnum::getCode, Function.identity()));

    /**
     * 根据code来获取枚举
     */
    public static MyEnum getByCode(int code) {
        return channelEnumMap.get(code);
    }

}